using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using JNPF.Basics.Models;
using JNPF.Basics.Logic;
using JNPF.Utils;
using System.Threading.Tasks;
using System.Linq;
using JNPF.Utils.Controller;
using JNPF.Basics.Logic.Common.Login;
using JNPF.Utils.Extensions;
using Microsoft.AspNetCore.Authorization;
using JNPF.Basics.Models.Permission.Dtos.User;

namespace JNPF.Basics.Controller
{
    [Route("api/Jiushi/[Controller]")]
    public class PartnerController : BaseApiController
    {
        private PartnerBll CustomerpartnerService = new PartnerBll();
        private BillRuleBll billRuleService = new BillRuleBll();
        private DataInterfaceBll dataInterfaceBll = new DataInterfaceBll();
        private UserBll userBll = new UserBll();
        private OrganizeBll organizeBll = new OrganizeBll();


        #region GET

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(int id)
        {
            var data = await CustomerpartnerService.GetInfo(id);
            var output = data.MapTo<PartnerUpOutput>();
            return Success(output);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList(PartnerListQueryInput input)
        {
            var data = await CustomerpartnerService.GetPageListBySql(input);
            var outputList = data.list.MapToList<PartnerUpOutput>();
            outputList.ForEach(o =>
            {
            });
            var output = new PageResult()
            {
                list = outputList,
                pagination = data.pagination
            };
            return Success(output);
        }


        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("GetListAdmin")]
        public async Task<ResponseResult> GetListAdmin(PartnerListQueryInput input)
        {
            input.isAdmin = true;
            var data = await CustomerpartnerService.GetPageListBySql(input);
            var outputList = data.list.MapToList<PartnerUpOutput>();
            outputList.ForEach(o =>
            {
            });
            var output = new PageResult()
            {
                list = outputList,
                pagination = data.pagination
            };
            return Success(output);
        }



        #endregion

        #region POST

        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="input">新建输入</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<ResponseResult> Create([FromBody] PartnerCrInput input)
        {
            if (await userBll.IsExistByAccount(input.mobilePhone.Trim(), null))
            {
                return Error("当前合伙人号码已存在，不能录入");
            }
            PartnerEntity partnerEntity = await CustomerpartnerService.GetInfoByMobilePhone(input.mobilePhone.Trim());
            if (partnerEntity != null && partnerEntity.Id > 0)
            {
                return Error("该合伙人手机号已存在！");
            }
            var entity = input.MapTo<PartnerEntity>();
            await CustomerpartnerService.Create(entity);
            return Success("新建成功");
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="id">参数</param>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpPost("Reviewer/{id}")]
        public async Task<ResponseResult> Reviewer(int id, [FromBody] PartnerReviewer input)
        {
            var entity = await CustomerpartnerService.GetInfo(id);
            if (entity == null)
            {
                return Error("当前信息有误");
            }
            await CustomerpartnerService.Reviewer(entity, input);

            return Success("审核成功");
        }



        /// <summary>
        /// 直接审核
        /// </summary>
        /// <param name="id">参数</param>
        /// <returns></returns>
        [HttpPost("ReviewerNew/{id}")]
        public async Task<ResponseResult> Reviewer(int id)
        {
            var entity = await CustomerpartnerService.GetInfo(id);
            if (entity == null)
            {
                return Error("当前信息有误");
            }
            if (await userBll.IsExistByAccount(entity.MobilePhone, null))
            {
                return Error("账户手机号已存在，审核失败");
            }
            UserextendEntity userextendEntity = await new UserextendBll().GetInfoByUserId(entity.UserId);

            PartnerReviewer input = new PartnerReviewer();

            input.drawProportion = userextendEntity.DrawProportion;
            input.comConfig = userextendEntity.ComConfig;

            await CustomerpartnerService.Reviewer(entity, input);

            return Success("审核成功");
        }




        /// <summary>
        /// 拒绝
        /// </summary>
        /// <param name="id">参数</param>
        /// <returns></returns>
        [HttpPost("Refuse/{id}")]
        public async Task<ResponseResult> Refuse(int id)
        {
            var entity = await CustomerpartnerService.GetInfo(id);
            entity.EnabledMark = 2;

            await CustomerpartnerService.Update(id, entity);

            return Success("审核成功");
        }



        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id">主键id</param>
        /// <param name="input">编辑输入</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(int id, [FromBody] PartnerCrInput input)
        {
            var entity = input.MapTo<PartnerEntity>();
            var isOk = await CustomerpartnerService.Update(id, entity);
            return isOk > 0 ? Success("修改成功") : Error("修改失败");
        }

        /// <summary>
        /// 删除接口
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(int id)
        {
            var entity = await CustomerpartnerService.GetInfo(id);
            var isOk = await CustomerpartnerService.Delete(entity);
            return isOk > 0 ? Success("删除成功") : Error("删除失败");
        }




        #endregion

    }
}